<template>
	<view class="home-farm-list">
		<fixed>
			<view @tap.stop="toChooseLocation"
				class="fill-base flex-center pt-lg pb-lg pl-md pr-md f-paragraph c-title">
				<view class="flex-1 flex-warp">
					<i class="iconfont icon-dingwei mr-sm" style="margin-top: 3rpx;" :style="{color:primaryColor}"></i>
					<view class="max-580 ellipsis">{{location.address || '定位中...'}}</view>
				</view>
				<i class="iconfont icon-right" style="font-size: 28rpx;"></i>
			</view>
		</fixed>

		<view @tap.stop="goDetail(index)"
			class="farm-item fill-base flex-center mt-md ml-md mr-md pd-lg box-shadow radius-24"
			v-for="(item,index) in list.data" :key="index">
			<image mode="aspectFill" lazy-load class="cover radius-24" :src="item.cover"></image>
			<view class="flex-1 ml-lg">
				<view class="flex-y-center f-title c-title mt-sm mb-sm">
					<view class="max-title ellipsis">{{item.title}}</view>
					<view class="status-btn flex-center c-caption ml-sm radius-10"
						:class="[{'fill-space':item.is_open==0},{'fill-body':item.is_open==1}]"
						:style="{color:item.is_open==1?primaryColor:''}">
						{{item.is_open==1?'营业中':'闭店中'}}
					</view>
				</view>
				<view class="star-fill-info rel">
					<view class="flex-warp star rel">
						<view class="item-star flex-center" v-for="(aitem,aindex) in 5" :key="aindex">
							<i class="iconfont icon-star-bold-fill"></i>
						</view>
					</view>
					<view class="star-fill abs" :style="{width: item.star_percent}">
						<view class="flex-warp">
							<view class="item-star flex-center" v-for="(aitem,aindex) in 5" :key="aindex">
								<i class="iconfont icon-star-bold-fill icon-font-color"
									:style="{backgroundImage: '-webkit-linear-gradient(90deg, #FDCD47, #FFC000)'}"></i>
							</view>
						</view>
					</view>
				</view>
				<view class="flex-y-center f-caption c-caption mt-sm"><i class="iconfont icon-dingwei mr-sm"></i>
					<view class="addr-text ellipsis">{{item.address}}</view>
				</view>
			</view>
		</view>
		<load-more :noMore="list.current_page>=list.last_page&&list.data.length>0" :loading="loading" v-if="loading">
		</load-more>
		<abnor v-if="!loading&&list.data.length<=0&&list.current_page==1"></abnor>

		<abnor @confirm="$util.checkAuth({ type: 'userLocation' })" :tip="[{ text: '定位失败，请开启地理位置授权后重试~', color: 0 }]"
			:button="[{ text: '开启定位' , type: 'confirm' }]" btnSize="" v-if="!loading && !location.lng"> </abnor>

		<view class="space-footer"></view>
	</view>
</template>

<script>
	import {
		mapState,
		mapActions,
		mapMutations
	} from "vuex"
	export default {
		components: {},
		data() {
			return {
				options: {},
				param: {
					page: 1,
				},
				list: {
					data: []
				},
				loading: true,
				popupInfo: {},
				lockTap: false
			}
		},
		computed: mapState({
			primaryColor: state => state.config.configInfo.primaryColor,
			subColor: state => state.config.configInfo.subColor,
			configInfo: state => state.config.configInfo,
			userInfo: state => state.user.userInfo,
			location: state => state.user.location,
		}),
		onLoad(options) {
			this.$util.showLoading()
			this.options = options
			this.initIndex()
		},
		onPullDownRefresh() {
			// #ifndef APP-PLUS
			uni.showNavigationBarLoading()
			// #endif
			this.initRefresh()
			uni.stopPullDownRefresh()
		},
		onReachBottom() {
			if (this.list.current_page >= this.list.last_page || this.loading) return
			this.param.page = this.param.page + 1
			this.loading = true
			this.getList()
		},
		methods: {
			...mapActions(['getShopCarList']),
			...mapMutations(['updateUserItem']),
			async initIndex() {
				let {
					location
				} = this
				if (!location.lat) {
					location = await this.$util.getBmapLocation()
					this.updateUserItem({
						key: 'location',
						val: location
					})
				}
				if (!this.location.lat) {
					this.loading = false
					this.$util.hideAll()
					return
				}
				this.getList()
			},
			initRefresh() {
				this.param.page = 1
				this.initIndex()
			},
			async getList() {
				let {
					list: oldList,
					param,
					location
				} = this
				let {
					lat,
					lng
				} = location
				param = Object.assign({}, param, {
					lat,
					lng,
				});
				let newList = await this.$api.home.farmerList(param);
				newList.data.map(item => {
					item.is_open = 1
					item.star_percent = (item.star * 1 / 5 * 100).toFixed(2) + '%'
				})
				if (this.param.page == 1) {
					this.list = newList
				} else {
					newList.data = oldList.data.concat(newList.data)
					this.list = newList
				}
				this.loading = false
				this.$util.hideAll()
			},
			// 选择地区
			async toChooseLocation(e) {
				await this.$util.checkAuth({
					type: 'userLocation'
				})
				let [, {
					address = '',
					longitude: lng = 0,
					latitude: lat = 0
				} = {}] = await uni.chooseLocation();
				if (!address) return
				let location = {
					lng,
					lat,
					address
				}
				this.updateUserItem({
					key: 'location',
					val: location
				})
				this.param.page = 1
				this.getList()
				this.$util.back()
			},
			// 详情
			async goDetail(index) {
				let {
					id
				} = this.list.data[index]
				let url = `/home/pages/farm/detail?id=${id}`
				this.$util.goUrl({
					url
				})
			}
		}
	}
</script>


<style lang="scss">
	.home-farm-list {
		.farm-item {
			.cover {
				width: 160rpx;
				height: 160rpx;
			}

			.icon-dingwei {
				font-size: 24rpx;
			}


			.status-btn {
				width: 80rpx;
				height: 36rpx;
				font-size: 20rpx;
			}

			.max-title {
				max-width: 370rpx;
			}

			.addr-text {
				max-width: 420rpx;
			}
		}
	}
</style>
